<?php
/**
 * @package site
 */

/**
 * Class which carries information about infrastructure.
 */
class SiteInf {
	/** Current filesystem context */
	private $pathPrefixFilesystem;
	/** Current web url context */
	private $pathPrefixWeb;
	
	/** path to images */
	const PATH_IMAGES = Constants::PATH_IMAGES;
	/** path to styles */
	const PATH_CSS = Constants::PATH_CSS;
	/** path to includes */
	const PATH_INCLUDES = Constants::PATH_INCLUDES;
	/** path to transalted texts */
	const PATH_LOCALIZE = Constants::PATH_LOCALIZE;
	
	/** Create instace */
	function __construct() {
		$this->pathPrefixFilesystem = $_SERVER['DOCUMENT_ROOT'] . preg_replace("@/[^/]*$@", "", $_SERVER['SCRIPT_NAME']);
		$this->pathPrefixWeb = preg_replace("@/[^/]*$@", "", $_SERVER['SCRIPT_NAME']);
	}
	
	/** Get web url path to images */
	function getPathWebImg() {
		return $this->pathPrefixWeb . "/" . self::PATH_IMAGES;
	}
	
	/** Get web url path to styles */
	function getPathWebCSS() {
		return $this->pathPrefixWeb . "/" . self::PATH_CSS;
	}
	
	/** Get filesystem path to includes */
	function getPathFilesystemIncludes() {
		return $this->pathPrefixFilesystem . "/" . self::PATH_INCLUDES . "/templates";
	}
	
	/** Get filesystem path to localized texts */
	function  getPathFilesystemLocalize() {
		return $this->pathPrefixFilesystem . "/" . self::PATH_LOCALIZE;
	}
	
	/** Get filesystem path to images */
	function getPathFilesystemImg() {
		return $this->pathPrefixFilesystem . "/" . self::PATH_IMAGES;
	}
	
	/** 
	 * Helper method used by other methods here.
	 * @return bool - wheter is or not
 	 */
	private function isFileName(string $origName, string $prefix, string $suffix) {
		return preg_match("/^$prefix.*$suffix\..*/", basename($origName));		
	}
	
	/**
	 * Decide if $origName is a filename of a full picture
	 *
	 * @param string $origName
	 * @return bool
	 */
	function isFullPicFileName(string $origName) {
		return $this->isFileName($origName, Policy::imageFullPicPrefix(), Policy::imageFullPicSuffix());
	}
	
	/**
	 * Decide if $origName is a filename of a thumbnail picture
	 *
	 * @param string $origName
	 * @return bool
	 */
	function isThumbnailFileName(string $origName) {
		return $this->isFileName($origName, Policy::imageThumbnailPrefix(), Policy::imageThumbnailSuffix());
	}
	
	/**
	 * Decide if $origName is a filename of a full picture in a thumb view
	 *
	 * @param string $origName
	 * @return bool
	 */
	function isFullPicThumbViewFileName(string $origName) {
		return $this->isFileName($origName, Policy::imageFullPicThumbViewPrefix(), Policy::imageFullPicThumbViewSuffix());
	}
	
	/**
	 * Decide if $origName is a filename of a thumbnail picture in a thumb view
	 *
	 * @param string $origName
	 * @return bool
	 */
	function isThumbPicThumbViewFileName(string $origName) {
		return $this->isFileName($origName, Policy::imageThumbPicThumbViewPrefix(), Policy::imageThumbPicThumbViewSuffix());
	}
	
	/**
	 * Helper methed used by other methods here.
	 *
	 * @param string $origName - original name
	 * @param string $prefix - prefix
	 * @param string $suffix - suffix
	 * @return string
	 */
	private function imgName(string $origName, string $prefix, string $suffix) {
		$fileName = basename($origName);
		$newThumb = preg_replace("/(.*)\.(.*)/", "$1" . $suffix . ".$2", $fileName);
		return $prefix . $newThumb;
	}
	
	/**
	 * Create full picture name according to conventions.
	 *
	 * @param string $origName - original file name
	 * @return string - new file name
	 */
	function imgFullPicName(string $origName) {
		return $this->imgName($origName, Policy::imageFullPicPrefix(), Policy::imageFullPicSuffix());
	}
	
	/**
	 * Create thumbnail picture name according to conventions.
	 *
	 * @param string $origName - original file name
	 * @return string - new file name
	 */
	function imgThumbnailName(string $origName) {
		return $this->imgName($origName, Policy::imageThumbnailPrefix(), Policy::imageThumbnailSuffix());
	}
	
	/**
	 * Create full picture of thumbnail view name according to conventions.
	 *
	 * @param string $origName - original file name
	 * @return string - new file name
	 */
	function imgFullPicThumbViewName(string $origName) {
		return $this->imgName($origName, Policy::imageFullPicThumbViewPrefix(), Policy::imageFullPicThumbViewSuffix());
	}
	
	
	/**
	 * Create thumbnail picture of thumbnail view name according to conventions.
	 *
	 * @param string $origName - original file name
	 * @return string - new file name
	 */
	function imgThumbPicThumbViewName(string $origName) {
		return $this->imgName($origName, Policy::imageThumbPicThumbViewPrefix(), Policy::imageThumbPicThumbViewSuffix());
	}
	
	/**
	 * Include template of offer edit page to the output.
	 */
	public function templateOfferEdit() {
		require $this->getPathFilesystemIncludes() . '/offeredit.php';
	}
	
	/**
	 * Include template of login page to the output.
	 */
	public function templateLogin() {
		require $this->getPathFilesystemIncludes() . '/login.php';
	}
	
	/**
	 * Include template of registration/user editing page to the output.
	 */
	public function templateRegistration() {
		require $this->getPathFilesystemIncludes() . '/useredit.php';
	}
	
	/**
	 * Include template of search page to the output.
	 */
	public function templateSearch() {
		require $this->getPathFilesystemIncludes() . '/search.php';
	}
	
	/**
	 * Include template of welcome page page to the output.
	 */
	public function templateWelcomePage() {
		require $this->getPathFilesystemIncludes() . '/welcomepage.php';
	}
	
	/**
	 * Include template of header to the output.
	 */
	public function templateHeader() {
		require $this->getPathFilesystemIncludes() . '/header.php';
	}
	
	/**
	 * Include template of settings to the output.
	 */
	public function templateSettings() {
		require $this->getPathFilesystemIncludes() . '/settings.php';
	}
	
/**
	 * Include template of footer to the output.
	 */
	public function templateFooter() {
		require $this->getPathFilesystemIncludes() . '/footer.php';
	}
	
	/**
	 * Include template of custom page to the output.
	 */
	public function templateCustom($what, $resolveShortcut = false) {
		$info = SiteContext::getSiteContext();
		$dbConnector = $info->getDBConnector();
		
		$settings = Settings::getSettings();
		
		if ($resolveShortcut) {
			$text = $settings->resolveCodeShortcut($settings->$what);
		} else {
			$text = $settings->$what;
		}
		$universalText = $text;
		require $this->getPathFilesystemIncludes() . '/universal.php';
	}
	
	/**
	 * Create url without some variables.
	 * @param array $removedVariables - array of variable name's which shouldn't be included in url
	 * 									example: array("variableName" => true);
	 * @return string - new url
	 */
	public function urlRelativeWithoutSomeVariables(array $removedVariablesAssoc) {
		$url = "";
		foreach (@$_GET as $var => $value) {
			if (!@isset($removedVariablesAssoc[$var])) {
				if ($url == "") {
					$url .= "?$var=$value";
				} else {
					$url .= "&amp;$var=$value";
				}
			}
		}
		return preg_replace("/".Constants::WEB_DEFAULTSCRIPT."$/", "", $_SERVER['SCRIPT_NAME']).$url;
	}
	
	/**
	 * get relative url e.g. /eaiis/index.php?q=hello
	 * @return string
	 */
	public function urlRelative() {
		return $_SERVER['REQUEST_URI'];
	}
	
	/**
	 * get relative url without parameters e.g. /eaiis/index.php
	 * @return string
	 */
	public function urlRelativeNoParams() {
		return $_SERVER['SCRIPT_NAME'];
	}
	
}
?>